package com.mobile.web.workOrder;

import com.mobile.service.enums.WorkOrderEvent;
import com.mobile.service.inter.WorkOrderStateMachineService;
import com.mobile.service.statemachine.WorkOrderStateContext;
import com.mobile.util.Result;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import com.mobile.web.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 工单状态机控制器
 */
@Slf4j
@RestController
@RequestMapping("/workorder/statemachine")
@Tag(name = "工单状态机管理", description = "工单状态流转相关接口")
public class WorkOrderStateMachineController extends BaseController {

    @Autowired
    private WorkOrderStateMachineService stateMachineService;

    @PostMapping("/transition")
    @Operation(summary = "执行工单状态流转", description = "根据工单ID和事件代码执行状态流转操作")
    public Result<Boolean> executeTransition(
            @Parameter(description = "工单ID", required = true, example = "1001") 
            @RequestParam Integer workOrderId,
            @Parameter(description = "事件代码", required = true, example = "SUBMIT_AUDIT") 
            @RequestParam String eventCode,
            @Parameter(description = "状态流转上下文信息", required = true) 
             WorkOrderStateContext context) {
        
        try {
            WorkOrderEvent event = WorkOrderEvent.valueOf(eventCode);
            context.setWorkOrderId(workOrderId);
            context.setCurrentUser(this.getUserCode());
            Result<Boolean> result = stateMachineService.executeTransition(workOrderId, event, context);
            return result;
            
        } catch (Exception e) {
            log.error("工单状态流转失败", e);
            return Result.error("工单状态流转失败: " + e.getMessage());
        }
    }

    @GetMapping("/available-events/{workOrderId}")
    @Operation(summary = "获取工单可执行的事件", description = "根据工单ID查询当前状态下可执行的所有事件")
    public Result<Set<WorkOrderEvent>> getAvailableEvents(
            @Parameter(description = "工单ID", required = true, example = "1001") 
            @PathVariable Integer workOrderId) {
        try {
            Set<WorkOrderEvent> events = stateMachineService.getAvailableEvents(workOrderId);
            return Result.success(events);
        } catch (Exception e) {
            log.error("获取可用事件失败", e);
            return Result.error("获取可用事件失败: " + e.getMessage());
        }
    }

    @GetMapping("/available-events")
    @Operation(summary = "根据状态码获取可执行的状态流程", description = "直接通过状态码查询该状态下可执行的所有事件，无需提供工单ID")
    public Result<Set<WorkOrderEvent>> getAvailableEventsByStatus(
            @Parameter(description = "工单状态码", required = true, example = "AUDITING", 
                      schema = @io.swagger.v3.oas.annotations.media.Schema(
                          allowableValues = {"SUBMITTED", "AUDITING", "RE_DISPATCH", "COMPLETED", "NEED_CORRECT", "COLLABORATING", "REJECTED"}
                      )) 
            @RequestParam String statusCode) {
        try {
            Set<WorkOrderEvent> events = stateMachineService.getAvailableEventsByStatus(statusCode);
            return Result.success(events);
        } catch (Exception e) {
            log.error("根据状态码获取可用事件失败: statusCode={}", statusCode, e);
            return Result.error("根据状态码获取可用事件失败: " + e.getMessage());
        }
    }
}